﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using ZkSurvey.DAL.SurveyMgr;

namespace ZkSurvey.BLL.SurveyMgr
{
    public class ExternalUserBl
    {
        public void CreateExternalUser(ExternalUser extUser)
        {
            ExternalUserDa.InsertExternalUser(extUser);
        }

        public List<ExternalUser> GetExternalUserList()
        {
            return ExternalUserDa.GetExternalUserList();
        }

        /// <summary>
        /// 根据Email查询外部用户
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public ExternalUser GetExternalUser(string email)
        {
            List<ExternalUser> extUsers = ExternalUserDa.GetExternalUserList();
            return extUsers.Find(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
        }

        public Participation GetUserParticipation(int extUserId, int surveyId)
        {
            List<Participation> partics = ParticipationDa.GetParticipationList(extUserId, surveyId);
            return partics.Count > 0 ? partics[0] : null;
        }

        public List<Participation> GetParticipationList(int surveyId)
        {
            List<Participation> partics = ParticipationDa.GetParticipationList(null, surveyId);
            foreach (Participation partic in partics)
            {
                partic.User = ExternalUserDa.GetExternalUser(partic.ExternalUserId);
            }
            return partics;
        }

        public Participation GetParticipation(int id)
        {
            return ParticipationDa.GetParticipation(id);
        }
    }
}
